package com.bonc.ioc.service.impl;

import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.OpinionDao;
import com.bonc.ioc.interceptor.OpinionInterceptor;
import com.bonc.ioc.model.Opinion;
import com.bonc.ioc.service.OpinionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindException;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


/**
 * @author wxf
 * @Title
 * @Package
 * @Description 意见信息业务
 * @date 2021/4/711:16
 */
@Service
@Slf4j
public class OpinionServiceImpl extends BaseService implements OpinionService {

    @Resource
    private OpinionDao opinionDao;

    @Override
    public void insertOpinion(String title,String opinionInfo) {
        Opinion opinion = new Opinion();
        opinion.setTitle(title);
        opinion.setOpinionInfo(opinionInfo);
        opinion.setSubmitUser("用户");
        opinion.setState(2);
        opinionDao.insertOpinion(opinion);
    }

    @Override
    public PageResult selectOpinionByPage(HashMap map) throws BindException {
        HashMap<String, Object> pageParams = this.getPageParams();
        map.putAll(pageParams);
        List<Opinion> opinions = opinionDao.selectOpinionByPage(map);
        return new PageResult(map,opinions);
    }

    @Override
    public void handleOpinion(String id) {
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("state",1);
        map.put("id",id);
        opinionDao.handleOpinion(map);
    }

    @Override
    public Opinion selectById(String id) {
        return opinionDao.selectById(id);
    }

    //每天晚上11点清理意见中的map
    @Scheduled(cron = "0 0 23 * * ?")
    public void clearInterceptorMap(){
        log.info("进入定时器任务,map数量集合:{}",OpinionInterceptor.countMap);
        log.info("进入定时器任务,map时间集合:{}",OpinionInterceptor.timeMap);
        OpinionInterceptor.countMap.clear();
        OpinionInterceptor.timeMap.clear();
    }
}
